package com.jss.lanouhn.fristapp.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.modles.LunBoTuH5;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class LBTWebViewActivity extends AppCompatActivity {
    private WebView webView;
    private String url ,url2;
    private TextView tv1,tv2,tv3;
    private LunBoTuH5 lunBoTuH5;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lbtweb_view);
        webView = (WebView) findViewById(R.id.wv_lbtWebView);
        tv1= (TextView) findViewById(R.id.tv1_lbtWeb_view);
        tv2= (TextView) findViewById(R.id.tv2_lbtWeb_view);
        tv3= (TextView) findViewById(R.id.tv3_lbtWeb_view);
        initEvent();
        initDate();
    }

    private void initEvent() {
        Bundle bundle = this.getIntent().getExtras();
        int longId = bundle.getInt("longId");
        url = "http://www.liwushuo.com/posts/" + longId;
        url2="http://api.liwushuo.com/v2/posts_v2/"+longId;
        WebSettings webSettings = webView.getSettings();
        webView.loadUrl(url);
        webSettings.setJavaScriptEnabled(true);
        webView.reload();
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }

    private void  initDate() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client=new OkHttpClient();
                Request request=new Request.Builder()
                        .url(url2)
                        .build();
                Call call=client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result=response.body().string();
                        Gson gson=new Gson();
                        lunBoTuH5=gson.fromJson(result,LunBoTuH5.class);
                        if(lunBoTuH5.getData()!=null){
                            handler.sendEmptyMessage(1);
                        }
                    }
                });
            }
        }).start();

    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                tv1.setText(lunBoTuH5.getData().getLikes_count()+"");
                tv2.setText(lunBoTuH5.getData().getShares_count()+"");
                tv3.setText(lunBoTuH5.getData().getComments_count()+"");
            }
        }
    };
}
